package com.ruoyi.web.controller.common;

import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class FilesUploadUtils {

    public static void main(String[] args) {
        String iosStr = "iPod,iPhone,iPad,Unknown";
        String iosStr2 = "iPhone 13";
        System.out.println(iosStr2.indexOf("iPhone"));
    }

    /**
     * 获取图片宽高等信息
     *
     * @param file
     * @return
     */
    public static Map<String, Integer> getImageInfo(File file) {
        Map<String, Integer> map = new HashMap<>();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            BufferedImage buff = ImageIO.read(file);
            map.put("width", buff.getWidth());
            map.put("height", buff.getHeight());
            fis.close();
            delteTempFile(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            delteTempFile(file);
        }
        return map;
    }

    /**
     * 根据字符串生成图片
     *
     * @param str      字符串
     * @param filePath 图片保存路径
     * @throws Exception
     */
    public static void createImage(String str, String filePath) throws Exception {
        if (str.length() > 2)
            str = str.substring(0, 2);
        createImage(str, new Font("宋体", Font.BOLD, 60),
                new File(filePath), 192, 192);
    }

    /**
     * 根据str,font的样式以及输出文件目录
     *
     * @param str     字符串
     * @param font    字体
     * @param outFile 输出文件位置
     * @param width   宽度
     * @param height  高度
     * @throws Exception
     */
    public static void createImage(String str, Font font, File outFile,
                                   Integer width, Integer height) throws Exception {
        // 创建图片
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_BGR);
        Graphics2D g = image.createGraphics();
        g.setClip(0, 0, width, height);
        // 先用颜色填充整张图片,也就是背景
        g.setColor(new Color(91, 91, 250));
        // 画一个矩形
        g.fillRect(0, 0, width, height);
        // 去除锯齿(当设置的字体过大的时候,会出现锯齿)
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 设置字体颜色
        g.setColor(Color.white);
        // 设置画笔字体
        g.setFont(font);
        /** 用于获得水平居中x */
        int size = font.getSize();  //文字大小
        int x = (width - size * str.length()) / 2;
        if (x < 0) {
            x = 0;
        }
        /** 用于获得垂直居中y */
        Rectangle clip = g.getClipBounds();
        FontMetrics fm = g.getFontMetrics(font);
        int ascent = fm.getAscent();
        int descent = fm.getDescent();
        int y = (clip.height - (ascent + descent)) / 2 + ascent;
        // 在指定坐标除添加文字
        g.drawString(str, x, y);
        g.dispose();
        // 输出png图片
        ImageIO.write(image, "png", outFile);
    }

    /**
     * MultipartFile 转 File
     *
     * @param file
     * @throws Exception
     */
    public static File multipartFileToFile(MultipartFile file) throws Exception {
        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }

    //获取流文件
    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除本地临时文件
     *
     * @param file
     */
    public static void delteTempFile(File file) {
        if (file != null) {
            File del = new File(file.toURI());
            del.delete();
        }
    }

}
